package com.example.demo.config;

import com.example.demo.service.listener.ChatMessageRedisListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

@Component
public class RedisConfig {

    @Autowired
    public ChatMessageRedisListener chatMessageRedisListener;

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        // 订阅频道 "chat-message"
        container.addMessageListener(chatMessageRedisListener, new ChannelTopic("chat-message"));

        return container;
    }
}
